package com.dongliushui.servlet;

import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.dongliushui.util.VelocityHelper;

/** 
 * @ClassName: InitWorkServlet
 * @Description: 该项目的初始化工作，以后所有的初始化任务可以在init方法中进行
 * @author zhutulang
 * @date 2016年2月3日
 * @version V1.0
 */
public class InitWorkServlet extends HttpServlet{

	/**
	 * @Fields serialVersionUID 
	 */
	private static final long serialVersionUID = 2507016882937545730L;
	
	private static final Logger log = Logger.getLogger(InitWorkServlet.class);
	
	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		VelocityHelper.init(getServerRealPath());
	}
	
	/** 
	 * <p>Title: getServerRealPath<／p>
	 * <p>Description:获取应用绝对地址 <／p>
	 * @return
	 * @author zhutulang
	 * @version 1.0
	 */
	private String getServerRealPath(){
	    String str = null;
	    str = getServletContext().getRealPath("/");
	    if (StringUtils.isBlank(str)){
		    try{
		        URL localURL = getServletContext().getResource("/");
		        str = localURL.getPath();
		    }catch (MalformedURLException localMalformedURLException){
		        log.fatal("未知服务器，无法获得发布应用绝对地址", localMalformedURLException);
		        throw new RuntimeException(localMalformedURLException);
		    }
	    }
	    log.info("获取应用绝对地址："+str);
	    return str;
	}

}
